package xyz.jpcr.covid19app.Fragment;

import android.content.Context;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import xyz.jpcr.covid19app.Adapter.RumorAdapter;
import xyz.jpcr.covid19app.Api.ApiClient;
import xyz.jpcr.covid19app.Model.RumorResult.Rumor;
import xyz.jpcr.covid19app.Model.RumorResult.RumorResult;
import xyz.jpcr.covid19app.R;

public class RefuteRumorsFragment extends Fragment {

    RecyclerView recyclerView;
    SwipeRefreshLayout swipeRefreshLayout;
    Context context;
    RumorAdapter rumorAdapter;
    ArrayList<Rumor> arrayList = new ArrayList<>();
    int pageNum = 1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_refute_rumors, container, false);
        recyclerView = view.findViewById(R.id.rumor_recycler_view);
        context = getContext();
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        swipeRefreshLayout = view.findViewById(R.id.rumor_swipeRefreshLayout);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getJson();
            }
        });
        swipeRefreshLayout.setRefreshing(true);
        getJson();
        return view;
    }

    private void getJson() {
        Call<RumorResult> call = ApiClient.getInstance().getApi().getRumor(pageNum,10);
        pageNum ++;
        call.enqueue(new Callback<RumorResult>() {
            @Override
            public void onResponse(Call<RumorResult> call, Response<RumorResult> response) {
                if(response.code() == 200 && response.body() != null) {
                    ArrayList<Rumor> tmp = response.body().getResults();
                    tmp.addAll(arrayList);
                    arrayList = tmp;
                    rumorAdapter = new RumorAdapter(context, arrayList);
                    recyclerView.setAdapter(rumorAdapter);
                }
                else {
                    Toast.makeText(context, context.getText(R.string.network_slow), Toast.LENGTH_SHORT).show();
                }
                swipeRefreshLayout.setRefreshing(false);
            }

            @Override
            public void onFailure(Call<RumorResult> call, Throwable t) {
                swipeRefreshLayout.setRefreshing(false);
                Log.e(context.toString(), "onFailure: " + t.getCause());
                Toast.makeText(context, context.getText(R.string.network_slow), Toast.LENGTH_SHORT).show();
            }
        });
    }
}